#!/usr/bin/env python3
"""
视频转码系统主程序入口

基于Python + PyQt6开发的高性能视频转码系统，
支持多GPU并行转码和实时监控的单体桌面应用。
"""

import sys
import os
import asyncio
from pathlib import Path

# 添加src目录到Python路径
sys.path.insert(0, str(Path(__file__).parent))

from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import QTimer
import qasync

from .ui.main_window import VideoTranscoderApp
from .utils.error_handling import setup_qt_environment, check_qt_dependencies


def main():
    """主程序入口"""
    # 设置Qt运行时环境
    setup_qt_environment()
    
    # 检查Qt依赖
    missing_deps = check_qt_dependencies()
    if missing_deps:
        print("Qt依赖检查失败:")
        for dep in missing_deps:
            print(f"  - {dep}")
        sys.exit(1)
    
    # 创建Qt应用
    app = QApplication(sys.argv)
    app.setApplicationName("视频转码系统")
    app.setApplicationVersion("0.1.0")
    app.setOrganizationName("VideoTranscoder")
    
    # 设置事件循环
    loop = qasync.QEventLoop(app)
    asyncio.set_event_loop(loop)
    
    try:
        # 创建主窗口
        main_window = VideoTranscoderApp()
        main_window.show()
        
        # 启动事件循环
        with loop:
            # 设置定时器用于异步任务
            timer = QTimer()
            timer.timeout.connect(lambda: None)
            timer.start(100)
            
            # 在事件循环中启动监控
            loop.call_soon(asyncio.create_task, main_window.start_monitoring())
            
            loop.run_forever()
            
    except Exception as e:
        print(f"应用程序错误: {e}")
        sys.exit(1)
    
    finally:
        loop.close()


if __name__ == "__main__":
    main()